package com.jinzw.jform.rows

import android.app.AlertDialog
import android.content.Context
import android.support.v7.widget.AppCompatTextView
import android.view.Gravity
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView

import com.jinzw.jform.R
import com.jinzw.jform.core.DimensionUtils
import com.jinzw.jform.core.DimensionUtils.dpToPx
import android.content.DialogInterface
import android.graphics.Color
import android.util.TypedValue
import com.jinzw.jform.core.ResultHandler


open class PaddingLineRow(context: Context) : AbstractTextRow<String>(context) {

    //will be created by onCreateView
    var dateView: TextView? = null
    var width:Int=10
    var potsion:Int =0

   private var value: String? = null
        set(value) {
            field = value
            dateView?.text = value
        }
    var defaultDate: String? = null





    /**
     * Change this lambda to have a custom Date Picker (on click)
     */


    protected var customizeDateView: ((PaddingLineRow, TextView) -> Unit) = { dateRow, textView ->



    }

    /**
     * Change this lambda to have a custom Date View
     */
    fun customizeDateView(block: (PaddingLineRow, TextView) -> Unit) {
        this.customizeDateView = block
    }

    init {
        validator = { v -> v != null }
        onCreateView<PaddingLineRow> {
            val layout = RelativeLayout(context)



            //Generated the line
            createTitleView()
            val titleLayoutParams = RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,dpToPx(width))
            titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            titleView?.layoutParams = titleLayoutParams
            titleView?.setBackgroundColor(Color.parseColor("#f3f3f3"))
            layout.addView(titleView)
            layout
        }
        addOnViewCreatedListener<PaddingLineRow> {
            dateView?.setOnClickListener {
                onValueClicked()
            }
        }
    }


        //    设置一个下拉的列表选择项




    /**
     * Use this lambda to change the visual aspect of the TitleView
     */
    protected fun createDateView(): TextView {
        dateView = AppCompatTextView(context)
        customizeDateView.invoke(this, this.dateView as TextView)
        return dateView as TextView
    }

    override fun onValueClicked() {
        val resultHandler = ResultHandler<String>()
        resultHandler.registerListener {
            value = it
        }
    }

    override fun value(): String? {
        return value
    }
}